﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ThaiHoa.Win.QLSX.Entity;
using ThaiHoa.Win.QLSX.Kho.UTI;
using ThaiHoa.Win.QLSX.DAL;

namespace ThaiHoa.Win.QLSX.Kho.UI
{
    public partial class frmVatTuSearch : Form
    {
        #region Fields
        KhoDBContext db;
        public event EventHandler<SearchVatTuEventArgs> Searched;
        List<MacVatTu> listMacVatTu;
        #endregion
        #region Constructor
        public frmVatTuSearch()
        {
            InitializeComponent();
        }
        #endregion
        #region Functions
        private void KhoiTao()
        {
            db = new KhoDBContext();
            listMacVatTu = new List<MacVatTu>();
            listMacVatTu.Add(new MacVatTu() { Tenmac = "Tất Cả" });
            listMacVatTu.AddRange(db.MacVatTu.Where(l => l.TrangThai).ToList());
            cbMacVatTu.DataSource = listMacVatTu;

            //Định dạng control
            gbNangCao.Height = 50;
            this.Height = 160;
            txtTen.Focus();
        }
        private void XoaTrang()
        {
            txtMa.Text = "";
            txtTen.Text = "";
            cbMacVatTu.SelectedItem = listMacVatTu.FirstOrDefault(l => l.Tenmac.Equals("Tất Cả"));
        }
        #endregion
        #region Events
        private void chkNangCao_CheckedChanged(object sender, EventArgs e)
        {
            if (chkNangCao.Checked)
            {
                gbNangCao.Height = 100;
                this.Height = 200;
            }
            else
            {
                gbNangCao.Height = 30;
                this.Height = 160;
            }
        }
        #endregion

        private void frmCongNhanSearch_Load(object sender, EventArgs e)
        {
            KhoiTao();

        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            if (Searched != null)
            {
                string ma = txtMa.Text;
                string ten = txtTen.Text;
                MacVatTu macVatTu = cbMacVatTu.SelectedItem as MacVatTu;
                Searched(this, new SearchVatTuEventArgs(ma,ten,macVatTu));
            }
        }

        private void frmVatTuSearch_Load(object sender, EventArgs e)
        {
            KhoiTao();
            txtMa.Focus();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            XoaTrang();
        }
    }
    public class SearchVatTuEventArgs : EventArgs
    {
        public string Ma{set;get;}
        public string Ten{set;get;}
        public MacVatTu MacVatTu{set;get;}

        public SearchVatTuEventArgs(string ma,string ten,MacVatTu macVatTu)
        {
            Ma = ma;
            Ten = ten;
            MacVatTu = macVatTu;
        }
    }
}
